package cn.pn.project.server;


import cn.pn.project.common.QueryUtil;
import cn.pn.project.mapper.user.UserDao;
import cn.pn.project.pojo.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserService {

    @Autowired
    private UserDao userDao;

    /**
     * 根据userId 修改i用户余额
     * @param u
     * @return
     */
    @PostMapping("updateUserBalance")
    public Boolean updateUserBalance(@RequestBody User u){
        return userDao.updateUserBalance(u.getBalance(),u.getUserId())==1;
    }

    /**
     * 根据用户id 修改用户积分
     * @param u
     * @return
     */
    @PostMapping("updateUserIntegral")
    public Boolean updateUserIntegral(@RequestBody User u){
        return userDao.updateUserIntegral(u.getIntegral(),u.getUserId())==1;
    }
    /**
     * 获取用户信息
     *
     * @param userId
     * @return
     */
    @PostMapping("findUserById")
    public User findUserById(@RequestBody Integer userId) throws RuntimeException, SQLException {
        User u = new User();
        u.setUserId(userId);
        List<User> list = userDao.selectAllUser(u);
        return (list != null && list.size() == 1) ? list.get(0) : null;
    }

    /**
     * 修改用户信息
     *
     * @param user 所有信息都必须有  id 不能改变
     * @return
     */
    @PostMapping("updateUser")
    public Boolean updateUser(@RequestBody User user) throws RuntimeException, SQLException {
        if (user == null)
            return null;
        if (user.getUserId() == null)
            return null;
        return userDao.updateUser(user) == 1;

    }

    /**
     * 根据手机号码加密码登陆
     *
     * @param user 手机号 密码
     * @return 返回boolean值
     */
    @PostMapping("findByPhoneAndPassword")
    public User findByPhoneAndPassword(@RequestBody User user) throws RuntimeException, SQLException {
        if (user == null)
            return null;
        if (user.getUserPwd() == null || user.getTelephone() == null)
            return null;

        List<User> list = userDao.selectAllUser(user);
        return (list != null && list.size() == 1) ? list.get(0) : null;

    }

    /**
     * 添加用户
     *
     * @param user
     * @return
     */
    @PostMapping("insertUser")
    public Boolean insertUser(@RequestBody User user) throws RuntimeException, SQLException {
        if (user == null)
            return false;
        if (user.getUserId() != null)
            return false;
        if(user.getUserPwd()!=null)
            user.setUserPwd(QueryUtil.MD5(user.getUserPwd()));
        return userDao.insertUser(user)==1;
    }

    /**
     * 根据手机号查询用户
     * @param telephone
     * @return
     */
    @PostMapping("findByTelephone")
    public User findByTelephone(@RequestParam("telephone") String telephone) throws RuntimeException, SQLException {
        if(telephone!=null&&telephone.length()==11){
            User u=new User();
            u.setTelephone(telephone);
            List<User> userList=userDao.selectAllUser(u);
            return userList!=null&&userList.size()==1?userList.get(0):null;
        }
        return null;
    }

    /**
     * 更新用户登陆时间
     * @param userId
     * @return
     */
    @PostMapping("updateLoginTimeByUserId")
    public Boolean updateLoginTimeByUserId(@RequestBody Integer userId){
        return userDao.updateLoginTimeByUserId(userId)==1;
    }
}
